import { NodePoolMgr } from '../../Framework/Scripts/Managers/NodePoolMgr';
import { _decorator, Component, Input, input, sys,Node, EventKeyboard, KeyCode, Prefab, instantiate, director,EventTarget} from 'cc';
import { GameApp } from '../../Game/Scripts/GameApp';
import { ResManager } from '../../Framework/Scripts/Managers/ResManager';
import { AbNames, CommonPkg, LoadingPkg } from '../../Game/Scripts/Config/ResConfig';
import { GameConfig } from '../../Game/Scripts/Config/GameCfg';
import { UIMgr } from '../../Framework/Scripts/Managers/UIMgr';
import { GameUtils } from '../../Framework/Scripts/Utils/GameUtils';
const { ccclass, property } = _decorator;

//全局管理器
globalThis.gameMgr = {};

//事件
const eventTarget = new EventTarget();
globalThis.gameMgr._eventTarget = eventTarget;

/**
 * 项目启动入口
 */
@ccclass('Boot')
export class Boot extends Component {
    public static Instance: Boot = null;
    @property({
        tooltip:"是否使用WebSocket"
    })
    private useWebSocket: boolean = true;

    @property({
        tooltip:"我的NodeJs服务"
    })
    public net1: boolean = true;

    @property({
        tooltip:"刘洪森的开发服务"
    })
    public net2: boolean = true;

    _rootNode: Node;
    GM: any;

    protected onLoad(): void {
        if(Boot.Instance !== null){
            this.destroy();
            return;
        }
        Boot.Instance = this;
        console.log('====Boot==onLoad==========')
        GameConfig.isUseWs = this.useWebSocket;

        globalThis.token = GameUtils.generateUniqueString();
        console.log('=========唯一标识位===========',globalThis.token);
    }     

    protected async start() {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);

        ResManager.inst.loadDirAssetboundle([CommonPkg,LoadingPkg],(now: number, total: number)=>{},()=>{
            console.log("======loadAb Common End========")
            this.StartUp();
        })
    }
    private onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.F1:
                this.GM.active = !this.GM.active;;
                break;
        }
    }
    protected onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }
    public StartUp() {
        this.InitFramework()
        this.InitLoading();
        if(this.useWebSocket){
            let platform = sys.platform;
            if(platform !== sys.Platform.WECHAT_GAME){
                this.CheckHotUpdate();
            }
        }
    }
    private InitLoading(){
        // 创建GM测试
        let parent = UIMgr.inst.getParent(0);
        let pre = ResManager.inst.getAsset(AbNames.Common,'Prefabs/GM',Prefab);
        let item:any = instantiate(pre);
        parent.addChild(item);
        this.GM = item;
        this.GM.active = false;

        //打开Loadding
        UIMgr.inst.ShowUIView(AbNames.GameLoading,'Prefabs/GameLoading',1)

        // SceneMgr.inst.goToLoading();
    }
    
    //检查更新
    private  CheckHotUpdate(): void {

    }

    // 初始化模块
    private InitFramework(): void {
        this._rootNode = director.getScene().getChildByName('__rootNode__');
        if (!this._rootNode) {
            this._rootNode = new Node();
            this._rootNode.name = '__rootNode__';
            director.getScene().addChild(this._rootNode); 
        }
        globalThis.gameMgr._rootNode = this._rootNode;
        this._rootNode.addComponent(NodePoolMgr).Init();
        this._rootNode.addComponent(GameApp).Init();
        if(this.useWebSocket){
            console.log('初始化网络模块')
        }
    }
}